import {SocketUtils} from "../../socket/SocketUtils";
import * as proto from "../../socket/proto/proto";
import {ItemOp} from "../../socket/proto/proto";
import * as config from "../../config/config";

export enum COMMON_CID {
  DIAMOND = 1,
  COIN = 2,
  PIECE_POWERFUL = 3,
}

export class UserModel extends cc.EventTarget {
  private _id: string;
  private _name: string;
  private _activeRole: number;
  private _activeSwim: number;
  private _dailyGift = 0;  // 每日礼包状态<0已领取,0未分享，1可领取
  private _newbieGift = 0;  // 新手礼包状态，<0已领取，>=0分享次数
  private _itemsMap: { [cid: number]: proto.IItem } = {};
  private _sevenLoginInfo = {loginDays: 1, states: [0, 0, 0, 0, 0, 0, 0]};
  private _signCounts: number = 0;
  private _isSignToday: boolean = false;
  private _switch: boolean = false
  public msg: proto.UserAck;

  constructor() {
    super();
    SocketUtils.getLogicSocket().on(proto.UserAck, (msg: proto.UserAck) => {
      msg.items.forEach(value => {
        this._itemsMap[value.cid] = value;
        this.emit(value.cid.toString())
      });
      this._activeSwim = msg.activeSwim;
      this._activeRole = msg.activeRole;
      this.newbieGift = msg.newGift;
      this._isSignToday = msg.todaySignIn === 1;
      this._signCounts = msg.signInDay;
      this._sevenLoginInfo.loginDays = msg.loginDays;
      this._switch = msg.switch === 1
      msg.sevenLoginState.forEach((state: proto.SevenLoginState, ix) => {
        this._sevenLoginInfo.states[state.day - 1] = state.flag;
      });
      this.msg = msg
    });

    SocketUtils.getLogicSocket().on(proto.EveryDayGiftRsp, (msg: proto.EveryDayGiftRsp) => {
      this.dailyGift = msg.statu;
    });

    SocketUtils.getLogicSocket().on(proto.ItemOperationsAck, (msg: proto.ItemOperationsAck) => {
      msg.itemOps.forEach(value => {
        this.addItem(value)
        this.emit(value.item.cid.toString())
      })
    });
    SocketUtils.getLogicSocket().on(proto.InviterNum, (msg: proto.InviterNum) => {
      this.msg.invite = msg.invite
      this.emit('invite-change')
    })
  }

  get switch(): boolean {
    return this._switch;
  }

  getItemNum(id: number) {
    if (this._itemsMap[id]) {
      return this._itemsMap[id].num;
    }
    return 0
  }

  private addItem(itemOp: proto.IItemOp) {
    //添加物品
    if (itemOp.type === 1) {
      //已有物品
      if (this._itemsMap[itemOp.item.cid]) {
        this._itemsMap[itemOp.item.cid].num += itemOp.item.num;
      }
      //缺少物品
      else {
        this._itemsMap[itemOp.item.cid] = itemOp.item;
      }
    }
    //删除物品
    else if (itemOp.type === 2) {
      if (this._itemsMap[itemOp.item.cid]) {
        this._itemsMap[itemOp.item.cid].num -= itemOp.item.num;
        //若物品数量为0，删除物品
        if (this._itemsMap[itemOp.item.cid].num <= 0) {
          delete this._itemsMap[itemOp.item.cid];
        }
      }
    }
  }

  sevenLoginSignOK() {
    if (this._sevenLoginInfo.states.length >= this._sevenLoginInfo.loginDays) {
      this._sevenLoginInfo.states[this._sevenLoginInfo.loginDays - 1] = 1;
    }
  }

  set isSignToday(value: boolean) {
    this._isSignToday = value;
  }

  set signCounts(value: number) {
    this._signCounts = value;
  }

  get newbieGift(): number {
    return this._newbieGift;
  }

  set newbieGift(state: number) {
    this._newbieGift = state;
    this.emit('newbieGift');
  }

  get newbieGiftValid(): boolean {
    return this._newbieGift >= 0;
  }

  get hasNewbieGiftReward(): boolean {
    // 新手礼包奖励是否可以领取
    return this._newbieGift >= config.getGift(config.NEWBIEGIFT, 'aims');
  }

  get dailyGift(): number {
    return this._dailyGift;
  }

  set dailyGift(state: number) {
    this._dailyGift = state;
    this.emit('dailyGift');
  }

  get dailyGiftValid(): boolean {
    return this._dailyGift >= 0;
  }

  get hasDailyGiftReward(): boolean {
    return this._dailyGift === 1;
  }

  get sevenLoginInfo(): { loginDays: number, states: number[] } {
    return this._sevenLoginInfo;
  }

  get sevenLoginValid(): boolean {
    let day = this._sevenLoginInfo.loginDays
    return day > 0 && day <= this._sevenLoginInfo.states.length;
  }

  get hasSevenLoginReward(): boolean {
    // 七日登录礼包是否可以领取
    if (this.sevenLoginValid) {
      let day = this._sevenLoginInfo.loginDays;
      return this._sevenLoginInfo.states[day - 1] === 0;
    }
    return false;
  }

  set activeRole(value: number) {
    this._activeRole = value;
  }

  set activeSwim(value: number) {
    this._activeSwim = value;
  }

  get activeRole(): number {
    return this._activeRole;
  }

  get activeSwim(): number {
    return this._activeSwim;
  }

  get itemsMap(): { [p: number]: proto.IItem } {
    return this._itemsMap;
  }


  get signCounts(): number {
    return this._signCounts;
  }

  get isSignToday(): boolean {
    return this._isSignToday;
  }

  bind(label: cc.Label, cid: COMMON_CID) {
    label.string = this.getItemNum(cid).toString()
    const self = this;

    function addListener(label: cc.Label, cid: COMMON_CID) {
      self.on(cid + '', () => {
        label.string = self.getItemNum(cid).toString()
      }, label)
    }

    addListener(label, cid)
  }

  unbind(label: cc.Label) {
    this.targetOff(label)
  }
}